package com.smartwebee.android.blespp.hospital;

import android.databinding.DataBindingUtil;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.google.gson.Gson;
import com.smartwebee.android.blespp.R;
import com.smartwebee.android.blespp.databinding.ActivityMyDevicesBinding;
import com.smartwebee.android.blespp.hospital.adapter.DevicesAdapter;
import com.smartwebee.android.blespp.http.HttpRequest;
import com.smartwebee.android.blespp.http.RequestInterface;
import com.smartwebee.android.blespp.http.VolleyReqQueue;
import com.smartwebee.android.blespp.resp.DevicesResp;
import com.smartwebee.android.blespp.resp.ScoreResp;
import com.smartwebee.android.blespp.utils.UIUtils;
import com.smartwebee.android.blespp.utils.Utils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MyDevicesActivity extends BaseActivity implements RequestInterface {
    ActivityMyDevicesBinding binding;
    private DevicesAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_my_devices);
        TAG = "MyDevicesActivity";
        requestQueue = VolleyReqQueue.getInstance(this).getRequestQueue();
        initView();
        initData();
    }

    private void initData() {
        httpGetDeviceList();
    }

    private void httpGetDeviceList() {
        showLoading();
        HttpRequest httpRequest = new HttpRequest(Request.Method.POST, Utils.getEsnList,
                this) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> map = new HashMap<String, String>();
                map.put("sn", Utils.registerResp.getData().getSn());
                return map;
            }
        };
        requestQueue.add(httpRequest);
    }

    private void initView() {
        binding.btnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        adapter = new DevicesAdapter(this, new ArrayList<String>());
        binding.listview.setAdapter(adapter);
    }

    @Override
    public void onSuccess(String id, String resp) {
        dismissLoading();
        DevicesResp devicesResp = new Gson().fromJson(resp, DevicesResp.class);
        List<String> mySids = new ArrayList<>();
        if (Utils.parseData(devicesResp)) {
            for (int i = 0; i < devicesResp.getData().size(); i++) {
                mySids.add(devicesResp.getData().get(0).getSid());
            }
            adapter.setItems(mySids);
        }

    }

    @Override
    public void onError(String error) {
        dismissLoading();
        UIUtils.showTips(this, error);
    }
}
